using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;

namespace cCtestApp
{
    public partial class Form1 : Form
    {

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        private string path; // path do ini filea

        delegate void pocasniDelegat(string key, string text, string prefix, string id);
        delegate void progresDelegat(int max);
        delegate void prikaziDelegat();


        private class kurs
        {
            public string ime, id;
            public kurs(string i, string im) { ime = im; id = i; }
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //courseCore.cCore c = new courseCore.cCore("42");
            int x = SystemInformation.WorkingArea.Size.Width - this.Size.Width;
            int y = SystemInformation.WorkingArea.Size.Height - this.Size.Height;
            this.Location = new Point(x, y);
            // kada klinemo na osvjezi kreiraj thread koji ce nam srediti kurseve
            Thread priv = new Thread(new ThreadStart(pokupiPodatke));
            priv.Start(); 
            
        }

        // Pronalazi aktivne kurseve iz opcije.ini
        private ArrayList nadjiAktivne()
        {
            string imeKursa, id;

            path = Application.StartupPath + "\\opcije.ini";
            StringBuilder temp = new StringBuilder(255);

            ArrayList priv = new ArrayList();

            // koliko ukupno ima kurseva
            int i = GetPrivateProfileString("opcije", "broj", "", temp, 1024, path);
            string br = temp.ToString();

            // Ucitajmo aktivne kurseve u arraylistu
            for (int x = 0; x < Convert.ToInt32(br); x++)
            {
                i = GetPrivateProfileString("kursevi", x.ToString(), "", temp, 1024, path);

                if (temp.ToString().StartsWith("1"))
                {
                    imeKursa = temp.ToString().Substring(2, temp.ToString().IndexOf("|") - 2);
                    id = temp.ToString().Remove(0, temp.ToString().IndexOf("|") + 1);

                    priv.Add(new kurs(id, imeKursa));
                }
            }
            return priv;
        }
        // biti ce delegat za unosenje nodova u treview
        private void unesi(string key, string text, string prefix, string id)
        {
            switch(prefix) {
                case "k": direktorij.Nodes.Add("k" + key, text, 0); break;
                default:
                    direktorij.Nodes["k" + key].Nodes.Add(prefix + id, text, 1);
                    direktorij.Nodes["k"+key].ExpandAll();
                    break;
            }
        }

        // biti ce delegat za progressbar (da se povecava/ posatviti max vrijednost)
        private void progres(int max)
        {
            if (max == -1) progressBar1.Value++;  else  progressBar1.Maximum = max;
        }

        private void prikaziRezultate()
        {
            this.Size= new Size(300, 290);
            direktorij.Size = new Size(294, 230);
            direktorij.Location = new Point(2, 3);
            this.Text = "Rezultati poredjenja";
            TopMost = false;
            this.Location = new Point(200, 200);
            statusProgress.Visible = false;
            direktorij.Visible = true;
        }

        private void pokupiPodatke()
        {
            pocasniDelegat del = new pocasniDelegat(unesi);
            progresDelegat pd = new progresDelegat(progres);
            prikaziDelegat pridel = new prikaziDelegat(prikaziRezultate);

            ArrayList aktivniKursevi = nadjiAktivne();

            // postavi max vriejdnost progressbara
            this.Invoke(pd, aktivniKursevi.Count);

            // Petlja koja prolazi kroz sve kurseve iz liste
            foreach (kurs k in aktivniKursevi)
            {
                // Kreiraj kurs na treeview-u
                this.Invoke(del, k.id, k.ime, "k","");

                // Nadji nove resurse
                try
                {
                    courseCore.cCore etfC = new courseCore.cCore(k.id);
                    foreach (courseCore.cCore.link obj in etfC.promjene)
                        switch (obj.tip)
                        {
                            case 0: this.Invoke(del, k.id, obj.naziv, "r", obj.id); break;
                            case 1: this.Invoke(del, k.id, obj.naziv, "d", obj.id); break;
                            case 2: this.Invoke(del, k.id, obj.naziv, "l", obj.id); break;
                        }
                }
                catch (Exception ex) { MessageBox.Show(ex.ToString()); }
                // kurs osvjezen povecaj progressbar 
                this.Invoke(pd,-1);
            }
            this.Invoke(pridel);              

        }

        private void direktorij_DoubleClick(object sender, EventArgs e)
        {
            if (direktorij.SelectedNode.Name.StartsWith("k"))
                System.Diagnostics.Process.Start("http://c2.etf.unsa.ba/course/view.php?id=" + direktorij.SelectedNode.Name.Remove(0, 1));
            else
            {
                switch (direktorij.SelectedNode.Name.Substring(0,1))
                {
                    case "r": System.Diagnostics.Process.Start("http://c2.etf.unsa.ba/mod/resource/view.php?id=" + direktorij.SelectedNode.Name.Remove(0, 1)); break;
                    case "d": System.Diagnostics.Process.Start("http://c2.etf.unsa.ba/mod/forum/discuss.php?d=" + direktorij.SelectedNode.Name.Remove(0, 1)); break;
                    case "l": System.Diagnostics.Process.Start("http://c2.etf.unsa.ba/course/view.php?id=" + direktorij.SelectedNode.Name.Remove(0, 1)); break;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(Application.StartupPath + "\\backup");
        }



    }
}